<?php
/**
 * Oryx Articles Model 
 * @version 0.3
 * @author Richard Adams
 */
class Article extends Model {
	
   /**
    * Table Record Count
    * @var int
    */
   private $table_record_count;
   
   /**
	 * Class Constructor
	 * @return 
	 */
   public function __construct()
   {
      parent::Model();
   }

   /**
    * Finds all Records
    * @return array
    * @param int $start[optional] the record to start with 
    * @param int $count[optional] number of records to return
    */
   public function findAll($start = NULL, $count = NULL) 
   {
      return $this->find(NULL, $start, $count);
   }

   /**
    * Find by a filter
    * @return array
    * @param array $filter_rules filter rules e.g. SQL statement 
    * @param object $start[optional] the record to start with
    * @param object $count[optional] number of records to return
    */
   public function findByFilter($filter_rules, $start = NULL, $count = NULL) 
   {
      return $this->find($filter_rules, $start, $count);
   }

   /**
    * Find record
    * @return array
    * @param array $filters[optional] filter rules e.g. SQL statement
    * @param int $start[optional] the record to start with
    * @param int $count[optional] number of records to return
    */
   public function find($filters = NULL, $start = NULL, $count = NULL) 
   {

      $results = array();

      $this->table_record_count = $this->db->count_all('ox_articles');


      // Filter could be an array or filter values or an SQL string.
      $where_clause = '';
      if ($filters) {
         if (is_string($filters)) {
            $where_clause = $filters;
         }
         elseif (is_array($filters)) {
            // Build your filter rules
            // ///////////////////////////////////////////////////////////////////////
            // NOTE: There are many ways to build the select code. (For example, you
            // NOTE: ...just pass the $filters array to where() like:
            // NOTE: ...   $this->db->where($filters);
            // NOTE: ...instead of the foreach loop below. However, it's added to
            // NOTE: ...allow further customisation.
            // ///////////////////////////////////////////////////////////////////////
            if (count($filters) > 0) {
               foreach ($filters as $field => $value) {
                  $this->db->where($field, $value);
               }
            }
         }

      }

      if ($start) {
         if ($count) {
            $this->db->limit($start, $count);
         }
         else {
            $this->db->limit($start);
         }
      }


      // ///////////////////////////////////////////////////////////////////////
      // NOTE: If you want the results ordered by a specific field, do it here.
      // ///////////////////////////////////////////////////////////////////////
      // $this->db->orderby();

      $query = $this->db->get('ox_articles');

      if ($query->num_rows() > 0) {
         // return $query->result_array();
         foreach ($query->result_array() as $row)      // Go through the result set
         {
            // Build up a list for each column from the database and place it in
            // ...the result set

			$query_results['article_id']		 = $row['article_id'];
			$query_results['title']		 = $row['title'];
			$query_results['url_title']		 = $row['url_title'];
			$query_results['excerpt']		 = $row['excerpt'];
			$query_results['content']		 = $row['content'];
			$query_results['status']		 = $row['status'];
			$query_results['date_created']		 = $row['date_created'];
			$query_results['date_updated']		 = $row['date_updated'];
			$query_results['section_id']		 = $row['section_id'];
			$query_results['image_id']		 = $row['image_id'];
			$query_results['user_id']		 = $row['user_id'];

			$results[]		 = $query_results;


         }

      }

      return $results;

   }

   /**
    * Retrieve record by primary key
    * @return array
    * @param int $idField record id 
    */
   public function retrieve_by_pkey($idField) {

      $results = array();

     

      $this->db->where('article_id', "$idField");
      $this->db->limit(1);
      $query = $this->db->get('ox_articles');


      if ($query->num_rows() > 0) {
         $row = $query->row_array();

		$query_results['article_id']		 = $row['article_id'];
		$query_results['title']		 = $row['title'];
		$query_results['url_title']		 = $row['url_title'];
		$query_results['excerpt']		 = $row['excerpt'];
		$query_results['content']		 = $row['content'];
		$query_results['status']		 = $row['status'];
		$query_results['date_created']		 = $row['date_created'];
		$query_results['date_updated']		 = $row['date_updated'];
		$query_results['section_id']		 = $row['section_id'];
		$query_results['image_id']		 = $row['image_id'];
		$query_results['user_id']		 = $row['user_id'];

		$results		 = $query_results;


      }
      else {
         $results = false;
      }

      return $results;
   }

   /**
    * Adds new record
    * @return int record id 
    * @param array $data
    */
   public function add($data) 
   {
      $this->db->insert('ox_articles', $data);
      return $this->db->insert_id();
   }

   /**
    * Updates record
    * @return 
    * @param int $keyvalue record id
    * @param array $data data to update
    */
   public function modify($keyvalue, $data) 
   {
      $this->db->where('article_id', $keyvalue);
      $this->db->update('ox_articles', $data);
   }

   /**
    * Delete record by primary key 
    * @return boolean
    * @param object $idField record id
    */
   public function delete_by_pkey($idField) 
   {   
      // ///////////////////////////////////////////////////////////////////////
      // TODO: Just to eliminate nasty mishaps, the delete query has been
      // TODO: ...deliberately disabled. Enable it if you mean to by uncommenting
      // TODO: ...the database calls below
      // ///////////////////////////////////////////////////////////////////////
      $this->db->where('article_id', $idField);
      $this->db->delete('ox_articles');

      return true;

   }

}

